package stark.app.function.drivetest;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.drivetest.lib.model.AqRecordProvider;
import com.stark.drivetest.lib.model.DriveAnsweredQuesManager;
import com.stark.drivetest.lib.model.DriveDataManager;
import com.stark.drivetest.lib.model.bean.DriveQuesIdx;
import com.stark.drivetest.lib.model.constant.DriveType;
import com.stark.drivetest.lib.model.constant.EnterAqType;
import com.stark.drivetest.lib.model.constant.SubjectType;
import com.stark.drivetest.lib.ui.AnswerQuesActivity;
import com.stark.drivetest.lib.ui.MyCollectActivity;
import com.stark.drivetest.lib.ui.MyErrActivity;
import com.stark.drivetest.lib.ui.TestRecordActivity;

import java.util.List;

import stark.app.R;
import stark.app.databinding.ActivityDriveTestDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.basic.inf.IRetCallback;

/**
 * Author：daizhongbin on 2021/12/28
 * Email：1027154338@qq.com
 * Desc:
 */
public class DriveTestDemoActivity extends BaseNoModelActivity<ActivityDriveTestDemoBinding> {
    @Override
    protected int onCreate() {
        return R.layout.activity_drive_test_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.btnJudge.setOnClickListener(v -> {
            DriveDataManager.getInstance().getJudgeQuesIdx(SubjectType.SUBJECT_1, DriveType.C1, new IRetCallback<List<DriveQuesIdx>>() {
                @Override
                public void onResult(List<DriveQuesIdx> result) {
                    AqRecordProvider.getInstance().createAnswerQuesRecord("科目1C1驾照类型判断题", result, EnterAqType.EXERCISE);
                    startActivity(AnswerQuesActivity.class);
                }
            });
        });
        mDataBinding.btnSingle.setOnClickListener(v -> {
            DriveDataManager.getInstance().getSingleQuesIdx(SubjectType.SUBJECT_1, DriveType.C1, new IRetCallback<List<DriveQuesIdx>>() {
                @Override
                public void onResult(List<DriveQuesIdx> result) {
                    AqRecordProvider.getInstance().createAnswerQuesRecord("科目1C1驾照类型单选题", result, EnterAqType.EXERCISE);
                    startActivity(AnswerQuesActivity.class);
                }
            });
        });
        mDataBinding.btnMulti.setOnClickListener(v -> {
            DriveDataManager.getInstance().getMultiQuesIdx(SubjectType.SUBJECT_1, DriveType.A1, new IRetCallback<List<DriveQuesIdx>>() {
                @Override
                public void onResult(List<DriveQuesIdx> result) {
                    AqRecordProvider.getInstance().createAnswerQuesRecord("科目1A1驾照类型多选题", result, EnterAqType.EXERCISE);
                    startActivity(AnswerQuesActivity.class);
                }
            });
        });
        mDataBinding.btnImg.setOnClickListener(v -> {
            DriveDataManager.getInstance().getImgQuesIdx(SubjectType.SUBJECT_1, DriveType.A1, new IRetCallback<List<DriveQuesIdx>>() {
                @Override
                public void onResult(List<DriveQuesIdx> result) {
                    AqRecordProvider.getInstance().createAnswerQuesRecord("科目1A1驾照类型图形题", result, EnterAqType.EXERCISE);
                    startActivity(AnswerQuesActivity.class);
                }
            });
        });
        mDataBinding.btnSimulate.setOnClickListener(v -> {
            DriveDataManager.getInstance().simulateTestIdx(SubjectType.SUBJECT_4, DriveType.C1, new IRetCallback<List<DriveQuesIdx>>() {
                @Override
                public void onResult(List<DriveQuesIdx> result) {
                    AqRecordProvider.getInstance().createAnswerQuesRecord("科目1C1驾照模拟测试", result, EnterAqType.SIMULATE_TEST);
                    startActivity(AnswerQuesActivity.class);
                }
            });
        });
        mDataBinding.btnMyCollect.setOnClickListener(v -> {
            startActivity(MyCollectActivity.class);
        });
        mDataBinding.btnMyErr.setOnClickListener(v -> {
            startActivity(MyErrActivity.class);
        });
        mDataBinding.btnTestRec.setOnClickListener(v -> {
            startActivity(TestRecordActivity.class);
        });
        mDataBinding.btnS1Count.setOnClickListener(v -> {
            clickS1Count();
        });
        mDataBinding.btnS4Count.setOnClickListener(v -> {
            clickS4Count();
        });
        mDataBinding.btnS1Collect.setOnClickListener(v -> {
            MyCollectActivity.start(this, SubjectType.SUBJECT_1);
        });
        mDataBinding.btnS1Err.setOnClickListener(v -> {
            MyErrActivity.start(this, SubjectType.SUBJECT_1);
        });
        mDataBinding.btnUpdate.setOnClickListener(v -> {
            showDialog("下载刷新中...");
            DriveDataManager.getInstance().updateDbData(new DriveDataManager.UpdateDbCallback() {
                @Override
                public void onUpdate(boolean success) {
                    dismissDialog();
                    ToastUtils.showShort(success ? "刷新成功" : "刷新失败");
                }
            });
        });
    }

    @Override
    protected void initData() {
        showDialog("初始化中...");
        DriveDataManager.getInstance().init(new DriveDataManager.InitCallback() {
            @Override
            public void onInit(boolean success) {
                dismissDialog();
            }
        });
    }

    private void clickS1Count() {
        //科目1累计答题数
        int s1AnsweredCount = DriveAnsweredQuesManager.getInstance().getSubject1AnsweredQuesCount();
        //数据库中所有的科目1的题目数量
        int allCount = DriveDataManager.getInstance().getTotalCount(SubjectType.SUBJECT_1, null);
        //未做题数
        int noAnsweredCount = allCount - s1AnsweredCount;
        ToastUtils.showShort("科目1累计答题数为" + s1AnsweredCount + ", 未做题数为" + noAnsweredCount);
    }

    private void clickS4Count() {
        //科目1累计答题数
        int s4AnsweredCount = DriveAnsweredQuesManager.getInstance().getSubject4AnsweredQuesCount();
        //数据库中所有的科目4的题目数量
        int allCount = DriveDataManager.getInstance().getTotalCount(SubjectType.SUBJECT_4, null);
        //未做题数
        int noAnsweredCount = allCount - s4AnsweredCount;
        ToastUtils.showShort("科目4累计答题数为" + s4AnsweredCount + ", 未做题数为" + noAnsweredCount);
    }
}
